﻿#include "HelloWorldScene.h"
#include "CCSprite3D\CCSprite3D.h"
//#include "AnimationEvent.h"
//#include "CCDragonBonesNode.h"
#include "ASGame.h"

USING_NS_CC;
USING_NS_AS;

class MySprite : public Sprite
{
public:
	static MySprite* create(const std::string &spritefilename)
	{
		auto sprite = new MySprite;
		sprite->initWithFile(spritefilename);
		sprite->autorelease();
		//GLProgram* program = ShaderCache::getInstance()->programForKey(kCCShader_PositionTexture);
		auto shader = CCShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE);
		//auto shader = CCShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR);
		sprite->setShaderProgram(shader);
		return sprite;
	}
	virtual void draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated) override;
	void onDraw(const kmMat4 &transform, bool transformUpdated);

protected:
	CustomCommand _customCommand;

};

void MySprite::draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated)
{
	_customCommand.init(_globalZOrder);
	_customCommand.func = CC_CALLBACK_0(MySprite::onDraw, this, transform, transformUpdated);
	renderer->addCommand(&_customCommand);
}

void MySprite::onDraw(const kmMat4 &transform, bool transformUpdated)
{
	Director::getInstance()->setDepthTest(true);
	getShaderProgram()->use();
	getShaderProgram()->setUniformsForBuiltins(transform);

	GL::blendFunc( _blendFunc.src, _blendFunc.dst );

	GL::bindTexture2D( _texture->getName() );
	//GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX );
	GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION|GL::VERTEX_ATTRIB_FLAG_TEX_COORDS);



	ccVertex3F vertices[24];
	ccVertex2F uv[24];
	glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, 0, vertices);
	glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, uv);
	float x = 0;
	float y = 0;
	float len = 100;
	/////////// front
	vertices[0+0*4] = vertex3(x-len,y-len,len);                
	vertices[1+0*4] = vertex3(x-len,y+len,len);
	vertices[2+0*4] = vertex3(x+len,y-len,len);
	vertices[3+0*4] = vertex3(x+len,y+len,len);
	uv[0+0*4] = vertex2(0, 0.5);
	uv[1+0*4] = vertex2(0, 0);
	uv[2+0*4] = vertex2(0.5, 0.5);
	uv[3+0*4] = vertex2(0.5, 0);
	////////// right
	vertices[0+1*4] = vertex3(x+len,y-len,len);                        
	vertices[1+1*4] = vertex3(x+len,y+len,len);
	vertices[2+1*4] = vertex3(x+len,y-len,-len);
	vertices[3+1*4] = vertex3(x+len,y+len,-len);
	uv[0+1*4] = vertex2(0.5, 1);
	uv[1+1*4] = vertex2(0.5, 0.5);
	uv[2+1*4] = vertex2(1, 1);
	uv[3+1*4] = vertex2(1, 0.5);
	///////// back
	vertices[0+2*4] = vertex3(x+len,y-len,-len);        
	vertices[1+2*4] = vertex3(x+len,y+len,-len);
	vertices[2+2*4] = vertex3(x-len,y-len,-len);
	vertices[3+2*4] = vertex3(x-len,y+len,-len);
	uv[0+2*4] = vertex2(0, 1);
	uv[1+2*4] = vertex2(0, 0);
	uv[2+2*4] = vertex2(1, 1);
	uv[3+2*4] = vertex2(1, 0);
	////////// left
	vertices[0+3*4] = vertex3(x-len,y-len,len);                        
	vertices[1+3*4] = vertex3(x-len,y+len,len);
	vertices[2+3*4] = vertex3(x-len,y-len,-len);
	vertices[3+3*4] = vertex3(x-len,y+len,-len);
	uv[0+3*4] = vertex2(0, 1);
	uv[1+3*4] = vertex2(0, 0);
	uv[2+3*4] = vertex2(1, 1);
	uv[3+3*4] = vertex2(1, 0);
	///////// top
	vertices[0+4*4] = vertex3(x+len,y+len,len);        
	vertices[1+4*4] = vertex3(x-len,y+len,len);
	vertices[2+4*4] = vertex3(x+len,y+len,-len);
	vertices[3+4*4] = vertex3(x-len,y+len,-len);
	uv[0+4*4] = vertex2(0.5, 0.5);
	uv[1+4*4] = vertex2(1, 0.5);
	uv[2+4*4] = vertex2(0.5, 1);
	uv[3+4*4] = vertex2(1, 1);
	///////// bottom
	vertices[0+5*4] = vertex3(x+len,y-len,len);        
	vertices[1+5*4] = vertex3(x-len,y-len,len);
	vertices[2+5*4] = vertex3(x+len,y-len,-len);
	vertices[3+5*4] = vertex3(x-len,y-len,-len);
	uv[0+5*4] = vertex2(0, 0);
	uv[1+5*4] = vertex2(0.5, 0);
	uv[2+5*4] = vertex2(0, 0.5);
	uv[3+5*4] = vertex2(0.5, 0.5);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 24);

}

Scene* HelloWorld::createScene()
{
	// 'scene' is an autorelease object
	auto scene = Scene::create();

	// 'layer' is an autorelease object
	auto layer = HelloWorld::create();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
	//////////////////////////////
	// 1. super init first
	if ( !Layer::init() )
	{
		return false;
	}

	Size visibleSize = Director::getInstance()->getVisibleSize();
	Point origin = Director::getInstance()->getVisibleOrigin();
#if 0
	/////////////////////////////
	// 2. add a menu item with "X" image, which is clicked to quit the program
	//    you may modify it.

	// add a "close" icon to exit the progress. it's an autorelease object
	auto closeItem = MenuItemImage::create(
		"CloseNormal.png",
		"CloseSelected.png",
		CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

	closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
		origin.y + closeItem->getContentSize().height/2));

	// create menu, it's an autorelease object
	auto menu = Menu::create(closeItem, NULL);
	menu->setPosition(Point::ZERO);
	this->addChild(menu, 1);

	/////////////////////////////
	// 3. add your codes below...

	// add a label shows "Hello World"
	// create and initialize a label

	auto label = LabelTTF::create("Hello World", "Arial", 24);

	// position the label on the center of the screen
	label->setPosition(Point(origin.x + visibleSize.width/2,
		origin.y + visibleSize.height - label->getContentSize().height));

	// add the label as a child to this layer
	this->addChild(label, 1);

	// add "HelloWorld" splash screen"
	auto sprite = Sprite::create("HelloWorld.png");

	// position the sprite on the center of the screen
	sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

	// add the sprite as a child to this layer
	//this->addChild(sprite, 0);

	auto mysprite = MySprite::create("untitled.png");
	mysprite->setPosition(600,00);
	mysprite->setRotation3D(Vertex3F(-60,60,60));
	mysprite->runAction(RepeatForever::create(RotateBy::create(10,Vertex3F(360,180,0))));
	addChild(mysprite);

	auto sprite3d = CCSprite3D::create("md2/bauul/bauul.md2","md2/bauul/bauul.jpg");
	sprite3d->setScale(6);
	sprite3d->setPosition(700,300);
	//sprite3d->setRotation3D(Vertex3F(-60,60,60));
	sprite3d->runAction(RepeatForever::create(RotateBy::create(10,Vertex3F(0,360,0))));
	addChild(sprite3d);

	DragonBonesNode *dbNode = DragonBonesNode::create("dragon/skeleton.xml", "dragon/texture.xml", "Dragon", "Dragon", "");
	this->addChild(dbNode);
	dbNode->addEventListener(dragonBones::AnimationEvent::LOOP_COMPLETE , [](dragonBones::Event *event){CCLOG("Event Triggered:%s", event->getType().c_str());} , "");
	dbNode->addEventListener(dragonBones::AnimationEvent::MOVEMENT_CHANGE , [](dragonBones::Event *event){CCLOG("Event Triggered:%s", event->getType().c_str());} , "");
	dbNode->addEventListener(dragonBones::AnimationEvent::START , [](dragonBones::Event *event){CCLOG("Event Triggered:%s", event->getType().c_str());} , "");
	dbNode->addEventListener(dragonBones::AnimationEvent::COMPLETE , [](dragonBones::Event *event){CCLOG("Event Triggered:%s", event->getType().c_str());} , "");

	dbNode->getDisplayNode()->setPosition(CCPoint(200, 200));
	dbNode->gotoAndPlay("walk");
	ASComponent* comp1 = new ASComponent();
	ASComponent* comp2 = new ASComponent();
	ASMobileComponent* compb3 = new ASMobileComponent();
	int comname = comp1->getComName();
	comname = comp2->getComName();
	comname = compb3->getComName();
	comp2 = compb3;
	comname = comp2->getComName();
	comname = comp2->getComNameStatic();
	comname = ASComponent::getComNameStatic();
	comname = ASMobileComponent::getComNameStatic();
#endif

	_updateLastTime = 0;
	_updateFixTime = 1/60.0f;
	_elapseTime = 0;


	Size winSize = Director::getInstance()->getWinSize();
	//ASEntityInitData entityInitData;
	ASMobileControler* pMobileControler = new ASMobileControler();
	ASPath path;
	path.AddWayPoint(Vector2D(130,130));
	path.AddWayPoint(Vector2D(160,130));
	path.AddWayPoint(Vector2D(160,160));
	path.AddWayPoint(Vector2D(190,190));
	path.AddWayPoint(Vector2D(230,130));
	path.AddWayPoint(Vector2D(300,190));
	path.AddWayPoint(Vector2D(430,230));
	path.AddWayPoint(Vector2D(530,330));
	path.AddWayPoint(Vector2D(460,400));
	path.AddWayPoint(Vector2D(580,430));
	path.AddWayPoint(Vector2D(430,530));
	path.AddWayPoint(Vector2D(230,330));
	//
	ASEntityMobileData mobiledata(Vector2D(1,0),Prm.VehicleMass,Prm.MaxSteeringForce,Prm.MaxSpeed,Pi/20);
	ASSteerComponent* pSteer1 = new ASSteerComponent(Prm.VehicleMass*0.5);
	ASEntity* pEntity1 = new ASEntity(0,0,&mobiledata);
	pEntity1->setCollideFlag(0x01);
	pEntity1->setBoundRadius(40);
	pEntity1->addComponent(pSteer1);
	pEntity1->setPosition(Vector2D(100,100));
	pSteer1->setAim(Vector2D(100,100));
	//pSteer1->getSteeringBehavior()->ArriveOn();
	_vEntities.push_back(pEntity1);
	Sprite* sprite1 = Sprite::create("skill01.png");
	sprite1->setOpacity(50);
	addChild(sprite1);
	_vSprites.push_back(sprite1);
	Sprite* spritehead1 = Sprite::create("head.png");
	spritehead1->setPosition((pEntity1->getHeading()*40).x,(pEntity1->getHeading()*40).y);
	sprite1->setScale(pEntity1->getBoundRadius()*2/sprite1->getContentSize().width);
	sprite1->addChild(spritehead1,0,1);
	pSteer1->getSteeringBehavior()->GetPath()->Set(path);//>CreateRandomPath(6,0,0,winSize.width,winSize.height);
	//pSteer1->getSteeringBehavior()->FollowPathOn();
	//pSteer1->getSteeringBehavior()->CrowdPathOn();
	//pSteer1->getSteeringBehavior()->ObstacleAvoidanceOn();
	//pSteer1->getSteeringBehavior()->ForwardResistanceOn();
	//pSteer1->getSteeringBehavior()->SeparationOn();
	pMobileControler->addMobile(pSteer1);
	

	ASSteerComponent* pSteer11 = new ASSteerComponent(Prm.VehicleMass*0.5);//new ASSteerComponent(Vector2D(1,0),Prm.VehicleMass,Prm.MaxSpeed,Prm.MaxSteeringForce,Prm.VehicleMass*0.5);
	ASEntity* pEntity11 = new ASEntity(0,0,&mobiledata);
	pEntity11->setCollideFlag(0x01);
	pEntity11->setBoundRadius(40);
	pEntity11->addComponent(pSteer11);
	pEntity11->setPosition(Vector2D(100,100));
	pSteer11->setAim(Vector2D(100,100));
	//pSteer1->getSteeringBehavior()->ArriveOn();
	_vEntities.push_back(pEntity11);
	Sprite* sprite11 = Sprite::create("skill01.png");
	sprite11->setOpacity(50);
	sprite11->setColor(ccc3(255,255,0));
	addChild(sprite11);
	_vSprites.push_back(sprite11);
	Sprite* spritehead11 = Sprite::create("head.png");
	spritehead11->setPosition((pEntity11->getHeading()*40).x,(pEntity11->getHeading()*40).y);
	sprite11->setScale(pEntity11->getBoundRadius()*2/sprite11->getContentSize().width);
	sprite11->addChild(spritehead11,0,1);
	pSteer11->getSteeringBehavior()->SetPath(pSteer1->getSteeringBehavior()->GetPath()->GetPath());//->CreateRandomPath(6,0,0,winSize.width,winSize.height);
	//pSteer11->getSteeringBehavior()->FollowPathOn();
	//pSteer11->getSteeringBehavior()->CrowdPathOn();
	//pSteer11->getSteeringBehavior()->ObstacleAvoidanceOn();
	//pSteer11->getSteeringBehavior()->ForwardResistanceOn();
	//pSteer11->getSteeringBehavior()->SeparationOn();
	pMobileControler->addMobile(pSteer11);

	ASSteerComponent* pSteer12 = new ASSteerComponent(Prm.VehicleMass*0.5);//new ASSteerComponent(Vector2D(1,0),Prm.VehicleMass,Prm.MaxSpeed,Prm.MaxSteeringForce,Prm.VehicleMass*0.5);
	ASEntity* pEntity12 = new ASEntity(0,0,&mobiledata);
	pEntity12->setCollideFlag(0x01);
	pEntity12->setBoundRadius(40);
	pEntity12->addComponent(pSteer12);
	pEntity12->setPosition(Vector2D(100,100));
	pSteer12->setAim(Vector2D(100,100));
	_vEntities.push_back(pEntity12);
	Sprite* sprite12 = Sprite::create("skill01.png");
	sprite12->setOpacity(50);
	sprite12->setColor(ccc3(0,0,255));
	addChild(sprite12);
	_vSprites.push_back(sprite12);
	Sprite* spritehead12 = Sprite::create("head.png");
	spritehead12->setPosition((pEntity12->getHeading()*40).x,(pEntity12->getHeading()*40).y);
	sprite12->setScale(pEntity12->getBoundRadius()*2/sprite12->getContentSize().width);
	sprite12->addChild(spritehead12,0,1);
	pSteer12->getSteeringBehavior()->SetPath(pSteer1->getSteeringBehavior()->GetPath()->GetPath());//->CreateRandomPath(6,0,0,winSize.width,winSize.height);
	//pSteer12->getSteeringBehavior()->FollowPathOn();
	//pSteer12->getSteeringBehavior()->CrowdPathOn();
	//pSteer12->getSteeringBehavior()->ObstacleAvoidanceOn();
	//pSteer12->getSteeringBehavior()->ForwardResistanceOn();
	pMobileControler->addMobile(pSteer12);
	

	ASSteerComponent* pSteer2 = new ASSteerComponent(Prm.VehicleMass*0.5);//new ASSteerComponent(Vector2D(0,-1),Prm.VehicleMass,Prm.MaxSpeed*2,Prm.MaxSteeringForce*2,Prm.VehicleMass);
	ASEntity* pEntity2= new ASEntity(0,0,&mobiledata);
	pEntity2->setCollideFlag(0x02);
	pEntity2->setBoundRadius(20);
	pEntity2->addComponent(pSteer2);
	//pSteer2->getSteeringBehavior()->OffsetPursuitOn(pSteer1,Vector2D(20,20));
	//pSteer2->getSteeringBehavior()->ObstacleAvoidanceOn();
	pMobileControler->addMobile(pSteer2);
	_vEntities.push_back(pEntity2);
	Sprite* sprite2 = Sprite::create("skill01.png");
	addChild(sprite2);
	//sprite2->setColor(Color3B(0,0,255));
	_vSprites.push_back(sprite2);
	Sprite* spritehead2 = Sprite::create("head.png");
	spritehead2->setPosition((pEntity2->getHeading()*20).x,(pEntity2->getHeading()*20).y);
	sprite2->setScale(pEntity2->getBoundRadius()*2/sprite2->getContentSize().width);
	sprite2->addChild(spritehead2,0,1);

	ASSteerComponent* pSteer3 = new ASSteerComponent(Prm.VehicleMass*0.5);//new ASSteerComponent(Vector2D(0,-1),Prm.VehicleMass,Prm.MaxSpeed*2,Prm.MaxSteeringForce*2,Prm.VehicleMass);
	ASEntity* pEntity3 = new ASEntity(0,0,&mobiledata);
	pEntity3->setCollideFlag(0x02);
	pEntity3->setBoundRadius(20);
	pEntity3->addComponent(pSteer3);
	//pSteer3->getSteeringBehavior()->OffsetPursuitOn(pSteer11,Vector2D(-20,20));
	//pSteer3->getSteeringBehavior()->ObstacleAvoidanceOn();
	pMobileControler->addMobile(pSteer3);
	_vEntities.push_back(pEntity3);
	Sprite* sprite3 = Sprite::create("skill01.png");
	addChild(sprite3);
	//sprite3->setColor(Color3B(255,0,0));
	_vSprites.push_back(sprite3);
	Sprite* spritehead3 = Sprite::create("head.png");
	spritehead3->setPosition((pEntity3->getHeading()*20).x,(pEntity3->getHeading()*20).y);
	sprite3->setScale(pEntity3->getBoundRadius()*2/sprite3->getContentSize().width);
	sprite3->addChild(spritehead3,0,1);

	for (int i = 0; i < 20; i++)
	{
		ASSteerComponent* pSteer4 = new ASSteerComponent(Prm.VehicleMass*0.5);//new ASSteerComponent(Vector2D(0,-1),Prm.VehicleMass,Prm.MaxSpeed*2,Prm.MaxSteeringForce*2,0.5);
		ASEntity* pEntity4 = new ASEntity(0,0,&mobiledata);
		pEntity4->setCollideFlag(0x02);
		pEntity4->setBoundRadius(20);
		pEntity4->setAttackRadius(120);
		pEntity4->addComponent(pSteer4);
		//pSteer4->getSteeringBehavior()->OffsetPursuitOn(pSteer11,Vector2D(-20,20));
		//pSteer4->getSteeringBehavior()->ObstacleAvoidanceOn();
		pMobileControler->addMobile(pSteer4);
		_vEntities.push_back(pEntity4);
		Sprite* sprite4 = Sprite::create("skill01.png");
		addChild(sprite4);
		sprite4->setColor(Color3B(255,0,0));
		_vSprites.push_back(sprite4);
		Sprite* spritehead4 = Sprite::create("head.png");
		spritehead4->setPosition((pEntity4->getHeading()*20).x,(pEntity4->getHeading()*20).y);
		sprite4->setScale(pEntity4->getBoundRadius()*2/sprite4->getContentSize().width);
		sprite4->addChild(spritehead4,0,1);
	}

	for (int i = 0; i < 25; i++)
	{
		ASSteerComponent* pSteer5 = new ASSteerComponent(Prm.VehicleMass*0.5);//new ASSteerComponent(Vector2D(0,-1),Prm.VehicleMass,Prm.MaxSpeed*2,Prm.MaxSteeringForce*2,0.5);
		ASEntity* pEntity5 = new ASEntity(0,0,&mobiledata);
		pEntity5->setCollideFlag(0x02);
		pEntity5->setBoundRadius(20);
		pEntity5->setAttackRadius(1);
		pEntity5->addComponent(pSteer5);
		pEntity5->setPosition(Vector2D(100,100));
		//pSteer5->getSteeringBehavior()->OffsetPursuitOn(pSteer11,Vector2D(-20,20));
		//pSteer5->getSteeringBehavior()->ObstacleAvoidanceOn();
		pMobileControler->addMobile(pSteer5);
		_vEntities.push_back(pEntity5);
		Sprite* sprite5 = Sprite::create("skill01.png");
		addChild(sprite5);
		sprite5->setColor(Color3B(0,0,255));
		_vSprites.push_back(sprite5);
		Sprite* spritehead5 = Sprite::create("head.png");
		spritehead5->setPosition((pEntity5->getHeading()*20).x,(pEntity5->getHeading()*20).y);
		sprite5->setScale(pEntity5->getBoundRadius()*2/sprite5->getContentSize().width);
		sprite5->addChild(spritehead5,0,1);
	}
	
	for (int i = 0; i < 10; i++)
	{
		ASSteerComponent* pSteer4 = new ASSteerComponent(Prm.VehicleMass*0.5);//new ASSteerComponent(Vector2D(0,-1),Prm.VehicleMass,Prm.MaxSpeed*2,Prm.MaxSteeringForce*2,0.5);
		ASEntity* pEntity4 = new ASEntity(0,0,&mobiledata);
		pEntity4->setCollideFlag(0x02);
		pEntity4->setBoundRadius(20);
		pEntity4->setAttackRadius(120);
		pEntity4->addComponent(pSteer4);
		pEntity4->setPosition(Vector2D(ASRandom::Rand()%(int)winSize.width,ASRandom::Rand()%(int)winSize.height));
		//pSteer4->getSteeringBehavior()->OffsetPursuitOn(pSteer11,Vector2D(-20,20));
		//pSteer4->getSteeringBehavior()->ObstacleAvoidanceOn();
		pMobileControler->addMobile(pSteer4);
		_vEntities.push_back(pEntity4);
		Sprite* sprite4 = Sprite::create("skill01.png");
		addChild(sprite4);
		sprite4->setColor(Color3B(255,0,0));
		_vSprites.push_back(sprite4);
		Sprite* spritehead4 = Sprite::create("head.png");
		spritehead4->setPosition((pEntity4->getHeading()*20).x,(pEntity4->getHeading()*20).y);
		sprite4->setScale(pEntity4->getBoundRadius()*2/sprite4->getContentSize().width);
		sprite4->addChild(spritehead4,0,1);

		ASSteerComponent* pSteer5 = new ASSteerComponent(Prm.VehicleMass*0.5);//new ASSteerComponent(Vector2D(0,-1),Prm.VehicleMass,Prm.MaxSpeed*2,Prm.MaxSteeringForce*2,0.5);
		ASEntity* pEntity5 = new ASEntity(0,0,&mobiledata);
		pEntity5->setCollideFlag(0x02);
		pEntity5->setBoundRadius(20);
		pEntity5->setAttackRadius(1);
		pEntity5->addComponent(pSteer5);
		pEntity5->setPosition(Vector2D(ASRandom::Rand()%(int)winSize.width,ASRandom::Rand()%(int)winSize.height));
		//pSteer5->getSteeringBehavior()->OffsetPursuitOn(pSteer11,Vector2D(-20,20));
		//pSteer5->getSteeringBehavior()->ObstacleAvoidanceOn();
		pMobileControler->addMobile(pSteer5);
		_vEntities.push_back(pEntity5);
		Sprite* sprite5 = Sprite::create("skill01.png");
		addChild(sprite5);
		sprite5->setColor(Color3B(0,0,255));
		_vSprites.push_back(sprite5);
		Sprite* spritehead5 = Sprite::create("head.png");
		spritehead5->setPosition((pEntity5->getHeading()*20).x,(pEntity5->getHeading()*20).y);
		sprite5->setScale(pEntity5->getBoundRadius()*2/sprite5->getContentSize().width);
		sprite5->addChild(spritehead5,0,1);

		pSteer5->getSteeringBehavior()->ApproachHorizonOn(pEntity4,10);
		pSteer4->getSteeringBehavior()->ApproachHorizonOn(pEntity5,10);
		
	}

	ASEntity* pObstacle = new ASEntity();
	pObstacle->setCollideFlag(0x03);
	pObstacle->setPosition(Vector2D(550,260));
	pObstacle->setBoundRadius(100);
	_vEntities.push_back(pObstacle);
	Sprite* spriteObstacle = Sprite::create("skill01.png");
	addChild(spriteObstacle);
	spriteObstacle->setColor(Color3B(55,50,50));
	spriteObstacle->setScale(pObstacle->getBoundRadius()*2/spriteObstacle->getContentSize().width);
	_vSprites.push_back(spriteObstacle);
	pMobileControler->addObstacle(pObstacle);
	
	
	


	auto listener1 = EventListenerTouchOneByOne::create();//´´½¨Ò»¸ö´¥Ãþ¼àÌý
    listener1->setSwallowTouches(true); //ÉèÖÃÊÇ·ñÏëÏÂ´«µÝ´¥Ãþ
    
    //Í¨¹ý lambda ±í´ïÊ½ Ö±½ÓÊµÏÖ´¥ÃþÊÂ¼þµÄ»Øµô·½·¨
	listener1->onTouchBegan = [&,pObstacle](Touch* touch, Event* event){
		for (std::vector<AS::ASEntity*>::iterator it = _vEntities.begin(); it != _vEntities.end(); it++)
		{
			ASSteerComponent* pComSteer = (ASSteerComponent*)(*it)->getComponent(ASSteerComponent::getComNameStatic());
			if (pComSteer)
			{
				if (pComSteer->getSteeringBehavior()->isApproachOn())
				{
					pComSteer->getSteeringBehavior()->ApproachOff();
					pComSteer->getSteeringBehavior()->SeparationOff();
				}
				else
				{
					pComSteer->getSteeringBehavior()->ApproachHorizonOn(pObstacle,(*it)->getAttackRadius());
					//pComSteer->getSteeringBehavior()->SeparationOn();
					//(*it)->setMaxSpeed((*it)->getMaxSpeed()*2);
				}
			}
		}
		_vEntities.begin();
		//ASSteerComponent* pComSteer = (ASSteerComponent*)pEntity1->getComponent(ASSteerComponent::getComNameStatic());
		//Point point = touch->getLocation();
		//if (pComSteer)
		//{
		//	pComSteer->setAim(Vector2D((int)point.x,(int)point.y));
		//}	
		return true;
    };
    
    listener1->onTouchMoved = [](Touch* touch, Event* event){
        
    };
    
    listener1->onTouchEnded = [](Touch* touch, Event* event){
		
		
    };
    
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);
	scheduleUpdate();

	return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
	return;
#endif

	Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
}

void HelloWorld::update(float dt)
{
	for (_updateLastTime+=dt;_updateLastTime>=_updateFixTime;_updateLastTime-=_updateFixTime)
	{
		for (unsigned int i=0;i<_vEntities.size();i++)
		{
			_vEntities[i]->entityUpdate(_updateFixTime);
			Vector2D pos = _vEntities[i]->getPosition();
			ASSteerComponent* com = (ASSteerComponent*)_vEntities[i]->getComponent(ASSteerComponent::getComNameStatic());
			
			_vSprites[i]->setPosition(pos.x,pos.y);
			Sprite* head = (Sprite*)_vSprites[i]->getChildByTag(1);
			if(head)
			{
				head->setPosition((_vEntities[i]->getHeading()*20).x+_vSprites[i]->getContentSize().width/2,(_vEntities[i]->getHeading()*20).y+_vSprites[i]->getContentSize().height/2);
				//head->setVisible(false);
				//Vector2D vhead = com->getHeading();
				//Vector2D v0(1,0);
				//_vSprites[i]->setRotation(360*acos(vhead.Dot(v0)) * vhead.Sign(v0)/Pi);	
			

				//(360*com->getHeading().Angle()/Pi);
			}
		}
	}
}